package com.wlw.layoutmanager.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wlw.layoutmanager.R;
import com.wlw.layoutmanager.adapters.SlideAdapter;
import com.wlw.layoutmanager.beans.SlideBean;
import com.wlw.layoutmanager.layoutManager.SlideLayoutManager;
import com.wlw.layoutmanager.slide.ItemConfig;
import com.wlw.layoutmanager.slide.ItemTouchHelperCallback;
import com.wlw.layoutmanager.slide.OnSlideListener;
import com.wlw.layoutmanager.widget.SmileView;

import java.util.ArrayList;
import java.util.List;

public class SlideFragment extends Fragment {
    private RecyclerView mRecyclerView;
    private SmileView mSmileView;
    private SlideLayoutManager mSlideLayoutManager;
    private ItemTouchHelper mItemTouchHelper;
    private ItemTouchHelperCallback<SlideBean> mItemTouchHelperCallback;
    private List<SlideBean> mList = new ArrayList<>();
    private int mLikeCount = 50;
    private int mDislikeCount = 50;
    private SlideAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_slide, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initListener();
    }

    private void initView(View view) {
        mRecyclerView = view.findViewById(R.id.recyclerView);
        mSmileView = view.findViewById(R.id.smileView);
        mSmileView.setLike(mLikeCount);
        mSmileView.setDisLike(mDislikeCount);

        mAdapter = new SlideAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        addData();


        mItemTouchHelperCallback = new ItemTouchHelperCallback<>(mAdapter, mList);
        mItemTouchHelper = new ItemTouchHelper(mItemTouchHelperCallback);

        mSlideLayoutManager = new SlideLayoutManager(mRecyclerView, mItemTouchHelper);
        mItemTouchHelper.attachToRecyclerView(mRecyclerView);
        mRecyclerView.setLayoutManager(mSlideLayoutManager);


    }

    private void addData() {
        int[] icons = {R.mipmap.header_icon_1, R.mipmap.header_icon_2, R.mipmap.header_icon_3,
                R.mipmap.header_icon_4, R.mipmap.header_icon_1, R.mipmap.header_icon_2};
        String[] titles = {"Acknowledging", "Belief", "Confidence", "Dreaming", "Happiness", "Confidence"};
        String[] says = {
                "Do one thing at a time, and do well.",
                "Keep on going never give up.",
                "Whatever is worth doing is worth doing well.",
                "I can because i think i can.",
                "Jack of all trades and master of none.",
                "Keep on going never give up.",
                "Whatever is worth doing is worth doing well.",
        };
        int[] bgs = {
                R.mipmap.img_slide_1,
                R.mipmap.img_slide_2,
                R.mipmap.img_slide_3,
                R.mipmap.img_slide_4,
                R.mipmap.img_slide_5,
                R.mipmap.img_slide_6
        };

        for (int i = 0; i < 6; i++) {
            mList.add(new SlideBean(bgs[i], titles[i], icons[i], says[i]));
        }
    }

    private void initListener() {
        mItemTouchHelperCallback.setOnSlideListener(new OnSlideListener<SlideBean>() {
            @Override
            public void onSliding(RecyclerView.ViewHolder viewHolder, float ratio, int direction) {

            }

            @Override
            public void onSlided(RecyclerView.ViewHolder viewHolder, SlideBean slideBean, int direction) {
                if (direction == ItemConfig.SLIDED_LEFT) {
                    mDislikeCount++;
                    mSmileView.setDisLike(mDislikeCount);
                    mSmileView.disLikeAnimation();
                } else {
                    mLikeCount++;
                    mSmileView.setLike(mLikeCount);
                    mSmileView.likeAnimation();
                }
            }

            @Override
            public void onClear() {
                addData();
            }
        });
    }

}
